From 38fc6a956a7ba362bf275fe32972a92101c08237 Mon Sep 17 00:00:00 2001 From: robertlipe Date: Sat, 20 Jul 2013 02:16:28 +0000 Subject: [PATCH] Another joint effort with Gerhard. Quit pretending that we have microsecond support. Be more honest and call it millisecond everywhere. Remove more explicit accesses to microseconds member. Still not quite zero, but getting closer and this is a good checkpoint. --- gpsbabel/cetus.cc | 2 +- gpsbabel/csv_util.cc | 6 +++--- gpsbabel/defs.h | 6 +++--- gpsbabel/destinator.cc | 8 +++++--- gpsbabel/ggv_log.cc | 8 ++++---- gpsbabel/gopal.cc | 3 +++ gpsbabel/itracku.cc | 6 ++---- gpsbabel/jtr.cc | 12 ++++++------ gpsbabel/magproto.cc | 4 ++-- gpsbabel/mapsend.cc | 5 +++-- gpsbabel/navilink.cc | 6 +++--- gpsbabel/nmea.cc | 10 ++++------ gpsbabel/ozi.cc | 2 +- gpsbabel/sbn.cc | 2 +- gpsbabel/src/core/datetime.h | 37 +++++++++++++----------------------- gpsbabel/stmwpp.cc | 12 +++++------- gpsbabel/unicsv.cc | 4 ++-- gpsbabel/vitosmt.cc | 4 ++-- gpsbabel/vitovtt.cc | 7 +++---- 19 files changed, 66 insertions(+), 78 deletions(-) diff --git a/gpsbabel/cetus.cc b/gpsbabel/cetus.cc index 93bbb8e10..58f9fb032 100644 --- a/gpsbabel/cetus.cc +++ b/gpsbabel/cetus.cc @@ -211,7 +211,7 @@ read_track_point(cetus_track_point_t* data, const time_t basetime) } wpt->SetCreationTime(basetime + - ((data->hour & 0x1F) * 3600) + (data->min * 60) + data->sec, (int)data->dsec * 10000); + ((data->hour & 0x1F) * 3600) + (data->min * 60) + data->sec, data->dsec * 10); return wpt; } diff --git a/gpsbabel/csv_util.cc b/gpsbabel/csv_util.cc index 8368133e7..28185d6df 100644 --- a/gpsbabel/csv_util.cc +++ b/gpsbabel/csv_util.cc @@ -1182,7 +1182,7 @@ xcsv_parse_val(const char* s, waypoint* wpt, const field_map_t* fmp, int s_len = strlen(s); if (s_len < 4) { /* less than 1 epochsecond, an unusual case */ - wpt->SetCreationTime(0,(int) atoi(s) * 1000); + wpt->SetCreationTime(0,(int) atoi(s)); } else { char buff[32]; int off = s_len - 3; @@ -1192,7 +1192,7 @@ xcsv_parse_val(const char* s, waypoint* wpt, const field_map_t* fmp, s += off; strncpy(buff, s, 3); buff[3] = '\0'; - wpt->SetCreationTime(t, (int) MILLI_TO_MICRO(atoi(buff))); + wpt->SetCreationTime(t, atoi(buff)); } } break; @@ -1946,7 +1946,7 @@ xcsv_waypt_pr(const waypoint* wpt) char tbuf[24]; writetime(tbuf, sizeof(tbuf), "%s", wpt->GetCreationTime(), false); char mbuf[32]; - snprintf(mbuf, sizeof(mbuf), "%s%03d", tbuf, wpt->microseconds / 1000); + snprintf(mbuf, sizeof(mbuf), "%s%03d", tbuf, wpt->GetCreationTime().msec()); writebuff(buff, "%s", mbuf); } break; diff --git a/gpsbabel/defs.h b/gpsbabel/defs.h index bae38ef4c..d76bc974d 100644 --- a/gpsbabel/defs.h +++ b/gpsbabel/defs.h @@ -532,9 +532,9 @@ public: } void SetCreationTime(time_t t) { creation_time = t; } - void SetCreationTime(time_t t, int us) { - creation_time = t; - microseconds = us; + void SetCreationTime(time_t t, int ms) { + creation_time.setTime_t(t); + creation_time = creation_time.addMSecs(ms); } gpsbabel::DateTime creation_time; int microseconds; /* Optional millionths of a second. */ diff --git a/gpsbabel/destinator.cc b/gpsbabel/destinator.cc index f56a08875..d35ac0c1a 100644 --- a/gpsbabel/destinator.cc +++ b/gpsbabel/destinator.cc @@ -332,8 +332,10 @@ destinator_read_trk(void) snprintf(buff, sizeof(buff), "%06d%.f", date, time); strptime(buff, "%d%m%y%H%M%S", &tm); - wpt->SetCreationTime(mkgmtime(&tm), - ((int)time % 1000) * 1000); + int millisecs = (int) time % 1000; + wpt->SetCreationTime(mkgmtime(&tm), millisecs); +// FIXME: this is papering over a problem somewhere... +wpt->microseconds = millisecs * 1000; if (wpt->fix > 0) { wpt->fix = (fix_type)(wpt->fix + 1); @@ -446,7 +448,7 @@ destinator_trkpt_disp(const waypoint* wpt) gbfputint32(date, fout); time = ((int)tm.tm_hour * 10000) + ((int)tm.tm_min * 100) + tm.tm_sec; - time = (time * 1000) + (wpt->microseconds / 1000); + time = (time * 1000) + (wpt->GetCreationTime().msec()); gbfputflt(time, fout); } else { gbfputint32(0, fout); /* Is this invalid ? */ diff --git a/gpsbabel/ggv_log.cc b/gpsbabel/ggv_log.cc index 5b385c54e..04efd7da0 100644 --- a/gpsbabel/ggv_log.cc +++ b/gpsbabel/ggv_log.cc @@ -139,7 +139,7 @@ ggv_log_read(void) wpt->longitude = xlon; WAYPT_SET(wpt, course, le_read16(&buf[16 + 0])); - int microseconds = 0; + int milliseconds = 0; if (ggv_log_ver == 10) { double secs; @@ -152,7 +152,7 @@ ggv_log_read(void) tm.tm_min = le_read16(&buf[16 + 16]); secs = le_read_double(&buf[16 + 18]); tm.tm_sec = (int)secs; - microseconds = (secs - tm.tm_sec) * 1000000; + milliseconds = lround((secs - tm.tm_sec) * 1000.0); } else { wpt->altitude = le_read16(&buf[16 + 4]); wpt->sat = (unsigned char)buf[16 + 14]; @@ -179,7 +179,7 @@ ggv_log_read(void) tm.tm_year -= 1900; if (tm.tm_mon > 0) { tm.tm_mon--; - wpt->SetCreationTime(mkgmtime(&tm), microseconds); + wpt->SetCreationTime(mkgmtime(&tm), milliseconds); } } @@ -238,7 +238,7 @@ ggv_log_track_head_cb(const route_head* trk) speed = waypt_speed(prev, wpt); } if (wpt->creation_time > 0) { - secs = (double)tm.tm_sec + ((double)wpt->microseconds / 1000000); + secs = (double)tm.tm_sec + (1000 * wpt->GetCreationTime().msec()); } gbfputint16((gbint16) latint, fout); diff --git a/gpsbabel/gopal.cc b/gpsbabel/gopal.cc index e66f4b360..d24434192 100644 --- a/gpsbabel/gopal.cc +++ b/gpsbabel/gopal.cc @@ -218,6 +218,9 @@ gopal_read(void) case 0: /* "-" */ /* unknown fields for the moment */ sscanf(c, "%lu", µsecs); wpt->SetCreationTime(microsecs / 1000000, microsecs % 1000000); +// FIXME: this is totally papering over a problem where creation time is +// being overwritten later. +wpt->microseconds = microsecs % 1000000; break; case 1: /* Time UTC */ sscanf(c,"%lf",&hmsd); diff --git a/gpsbabel/itracku.cc b/gpsbabel/itracku.cc index 918abd72b..649061f31 100644 --- a/gpsbabel/itracku.cc +++ b/gpsbabel/itracku.cc @@ -661,15 +661,13 @@ itracku_rt_init(const char* fname) static void nmea_set_waypoint_time(waypoint* wpt, struct tm* time, double fsec) { - //fractions are stored as ms - int us = MILLI_TO_MICRO(lround(1000*fsec)); if (time->tm_year == 0) { - wpt->SetCreationTime(((((time_t)time->tm_hour * 60) + time->tm_min) * 60) + time->tm_sec, us); + wpt->SetCreationTime(((((time_t)time->tm_hour * 60) + time->tm_min) * 60) + time->tm_sec, lround(1000.0 * fsec)); if (wpt->wpt_flags.fmt_use == 0) { wpt->wpt_flags.fmt_use = 1; } } else { - wpt->SetCreationTime(mkgmtime(time), us); + wpt->SetCreationTime(mkgmtime(time), lround(1000.0 * fsec)); if (wpt->wpt_flags.fmt_use != 0) { wpt->wpt_flags.fmt_use = 0; } diff --git a/gpsbabel/jtr.cc b/gpsbabel/jtr.cc index 583c6c8b7..b4ec4d8d8 100644 --- a/gpsbabel/jtr.cc +++ b/gpsbabel/jtr.cc @@ -41,7 +41,7 @@ static gbfile* fin, *fout; static avltree_t* trkpts; static time_t -jtr_parse_time(const char* str, struct tm* tm, int* micro) +jtr_parse_time(const char* str, struct tm* tm, int* milli) { long int hms; char* dot; @@ -54,8 +54,8 @@ jtr_parse_time(const char* str, struct tm* tm, int* micro) hms = hms / 100; tm->tm_hour = hms % 100; - if ((*dot == '.') && (micro != NULL)) { - *micro = atoi(dot + 1) * 10000; + if ((*dot == '.') && (milli != NULL)) { + *milli = atoi(dot + 1) * 10; } return mkgmtime(tm); @@ -115,7 +115,7 @@ jtr_read(void) double lat, lon; float speed, course, mcourse, mvar, mdev; time_t time = 0; - int micros = 0; + int mills = 0; char buf[32]; char mvardir, mdevdir; @@ -148,7 +148,7 @@ jtr_read(void) case 0: break; /* GEOTAG2 */ case 1: - jtr_parse_time(str, &tm, µs); + jtr_parse_time(str, &tm, &mills); break; case 2: valid = *str; @@ -219,7 +219,7 @@ jtr_read(void) wpt->latitude = lat; wpt->longitude = lon; - wpt->SetCreationTime(time, micros); + wpt->SetCreationTime(time, mills); if (speed >= 0) { WAYPT_SET(wpt, speed, speed); } diff --git a/gpsbabel/magproto.cc b/gpsbabel/magproto.cc index 984922bb9..d9bdd701f 100644 --- a/gpsbabel/magproto.cc +++ b/gpsbabel/magproto.cc @@ -993,7 +993,7 @@ mag_trkparse(char* trkmsg) dmy = dmy / 100; tm.tm_mday = dmy % 100; - waypt->SetCreationTime(mkgmtime(&tm), CENTI_TO_MICRO(fracsecs)); + waypt->SetCreationTime(mkgmtime(&tm), 10.0 * fracsecs); if (latdir == 'S') { latdeg = -latdeg; @@ -1449,7 +1449,7 @@ void mag_track_disp(const waypoint* waypointp) tm->tm_sec; date = tm->tm_mday * 10000 + tm->tm_mon * 100 + tm->tm_year; - fracsec = MICRO_TO_CENTI(waypointp->microseconds); + fracsec = lround(waypointp->GetCreationTime().msec()/10.0); } } if (!tm) { diff --git a/gpsbabel/mapsend.cc b/gpsbabel/mapsend.cc index 8c40711de..31c1b91e9 100644 --- a/gpsbabel/mapsend.cc +++ b/gpsbabel/mapsend.cc @@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ +#include #include #include @@ -223,7 +224,7 @@ mapsend_track_read(void) } else { centisecs = 0; } - wpt_tmp->SetCreationTime(t, CENTI_TO_MICRO(centisecs)); + wpt_tmp->SetCreationTime(t, 10.0 * centisecs); track_add_wpt(track_head, wpt_tmp); } @@ -514,7 +515,7 @@ void mapsend_track_disp(const waypoint* wpt) /* 0 centiseconds */ if (trk_version >= 30) { - c = MICRO_TO_CENTI(wpt->microseconds); + c = lround(wpt->GetCreationTime().msec() / 10.0); gbfwrite(&c, 1, 1, mapsend_file_out); } } diff --git a/gpsbabel/navilink.cc b/gpsbabel/navilink.cc index cba283ae9..12c6a9557 100644 --- a/gpsbabel/navilink.cc +++ b/gpsbabel/navilink.cc @@ -796,10 +796,10 @@ serial_write_route_end(const route_head* route) } static int -decode_sbp_usec(const unsigned char* buffer) +decode_sbp_msec(const unsigned char* buffer) { int msec = le_read16(buffer); - return (msec % 1000) * 1000; + return (msec % 1000); } static time_t @@ -852,7 +852,7 @@ navilink_decode_logpoint(const unsigned char* buffer) waypt->hdop = ((unsigned char)buffer[0]) * 0.2f; waypt->sat = buffer[1]; waypt->SetCreationTime(decode_sbp_datetime_packed(buffer + 4), - decode_sbp_usec(buffer + 2)); + decode_sbp_msec(buffer + 2)); decode_sbp_position(buffer + 12, waypt); WAYPT_SET(waypt, speed, le_read16(buffer + 24) * 0.01f); WAYPT_SET(waypt, course, le_read16(buffer + 26) * 0.01f); diff --git a/gpsbabel/nmea.cc b/gpsbabel/nmea.cc index fb4020308..85af2da42 100644 --- a/gpsbabel/nmea.cc +++ b/gpsbabel/nmea.cc @@ -358,16 +358,14 @@ nmea_wr_deinit(void) static void nmea_set_waypoint_time(waypoint* wpt, struct tm* time, double fsec) { - //fractions are stored as ms - int us = MILLI_TO_MICRO(lround(1000*fsec)); if (time->tm_year == 0) { - wpt->SetCreationTime(((((time_t)time->tm_hour * 60) + time->tm_min) * 60) + time->tm_sec, us); + wpt->SetCreationTime(((((time_t)time->tm_hour * 60) + time->tm_min) * 60) + time->tm_sec, lround(1000.0 * fsec)); if (wpt->wpt_flags.fmt_use == 0) { wpt->wpt_flags.fmt_use = 1; without_date++; } } else { - wpt->SetCreationTime(mkgmtime(time), us); + wpt->SetCreationTime(mkgmtime(time), lround(1000.0 * fsec)); if (wpt->wpt_flags.fmt_use != 0) { wpt->wpt_flags.fmt_use = 0; without_date--; @@ -1301,7 +1299,7 @@ nmea_trackpt_pr(const waypoint* wpt) if (opt_gprmc) { snprintf(obuf, sizeof(obuf), "GPRMC,%010.3f,%c,%08.3f,%c,%09.3f,%c,%.2f,%.2f,%06d,,", - (double) hms + (wpt->microseconds / 1000000.0), + (double) hms + (wpt->GetCreationTime().msec() / 1000.0), fix=='0' ? 'V' : 'A', fabs(lat), lat < 0 ? 'S' : 'N', fabs(lon), lon < 0 ? 'W' : 'E', @@ -1320,7 +1318,7 @@ nmea_trackpt_pr(const waypoint* wpt) } if (opt_gpgga) { snprintf(obuf, sizeof(obuf), "GPGGA,%010.3f,%08.3f,%c,%09.3f,%c,%c,%02d,%.1f,%.3f,M,0.0,M,,", - (double) hms + (wpt->microseconds / 1000000.0), + (double) hms + (wpt->GetCreationTime().msec() / 1000.0), fabs(lat), lat < 0 ? 'S' : 'N', fabs(lon), lon < 0 ? 'W' : 'E', fix, diff --git a/gpsbabel/ozi.cc b/gpsbabel/ozi.cc index 6dd4b7edf..48cd0b23e 100644 --- a/gpsbabel/ozi.cc +++ b/gpsbabel/ozi.cc @@ -165,7 +165,7 @@ ozi_set_time_str(const char *str, waypoint *waypointp) if(ozi_time > DAYS_SINCE_1990) { waypointp->SetCreationTime((ozi_time - DAYS_SINCE_1990) * SECONDS_PER_DAY, - lround(1000000.0 * (ozi_time - (int) ozi_time))); + lround(1000.0 * (ozi_time - (int) ozi_time))); } } diff --git a/gpsbabel/sbn.cc b/gpsbabel/sbn.cc index ea524db9d..35af7d63c 100644 --- a/gpsbabel/sbn.cc +++ b/gpsbabel/sbn.cc @@ -225,7 +225,7 @@ decode_sbn_datetime(const unsigned char *buffer, waypoint *waypt) tm.tm_mon = buffer[2] - 1; tm.tm_year = be_readu16(buffer) - 1900; - waypt->SetCreationTime(mkgmtime(&tm), (ms % 1000) * 1000); + waypt->SetCreationTime(mkgmtime(&tm), (ms % 1000)); } static void diff --git a/gpsbabel/src/core/datetime.h b/gpsbabel/src/core/datetime.h index cebb55b94..d7c535683 100644 --- a/gpsbabel/src/core/datetime.h +++ b/gpsbabel/src/core/datetime.h @@ -43,51 +43,40 @@ public: t_ = -1; } - DateTime(QDate date, QTime time) : QDateTime(date, time) { } - DateTime(QDateTime dt) : QDateTime(dt) { } - + DateTime(QDate date, QTime time) : QDateTime(date, time) {} + DateTime(QDateTime dt) : QDateTime(dt) {} // Handle time_tm tm = wpt->creation_time; - operator const time_t() const { - return this->toTime_t(); - } + operator const time_t() const { return this->toTime_t(); } const time_t& operator=(const time_t& t) { this->setTime_t(t); return t; } - time_t operator-- (int) { + time_t operator--(int) { setTime_t(toTime_t() - 1); return this->toTime_t(); } - time_t operator++ (int) { + time_t operator++(int) { setTime_t(toTime_t() + 1); return this->toTime_t(); } - time_t operator+=(const time_t&t) { - setTime_t(toTime_t() + t); + time_t operator+=(const time_t& t) { + setTime_t(toTime_t() + t); return this->toTime_t(); } // Handle tm = *gmtime(&wpt->creation_time) ...poorly. - inline const time_t* operator&() { + inline const time_t *operator&() { t_ = this->toTime_t(); -//fprintf(stderr, "inline set time_t %d\n", t_); return &t_; } - // Before Qt, GPSBabel had a 'microseconds' which is excessive and - // not really supported in QDateTime. Milliseconds is fine, but we - // provide these shims for code that used usecs. - void addUSecs(qint64 usecs) const { - this->addMSecs(usecs / 1000); - } - int usec() const { - return this->time().msec() * 1000; - } + // A convenience method to return the number of milliseconds (0-999). + int msec() const { return this->time().msec(); } // Integer form: YYMMDD int ymd() const { @@ -104,10 +93,11 @@ public: QTime time(this->time()); return time.hour() * 10000 + time.minute() * 100 + time.second(); } + // Qt 4.6 and under doesn't have msecsTo. Fortunately, it's easy to // provide. It's a 64-bit because if the times aren't on the same day, // the returned value can be quite large. - int64_t msecsTo(const QDateTime& dt) { + int64_t msecsTo(const QDateTime &dt) { qint64 days = this->daysTo(dt); qint64 msecs = this->time().msecsTo(dt.time()); return days * (1000 * 3600 * 24) + msecs; @@ -124,9 +114,8 @@ public: } return this->toUTC().toString(format); } - - private: +private: time_t t_; }; diff --git a/gpsbabel/stmwpp.cc b/gpsbabel/stmwpp.cc index 291266137..30e9e9f6c 100644 --- a/gpsbabel/stmwpp.cc +++ b/gpsbabel/stmwpp.cc @@ -103,11 +103,10 @@ stmwpp_data_read(void) wpt = NULL; memset(&time, 0, sizeof(time)); - int microseconds = 0; + int milliseconds = 0; while ((c = csv_lineparse(buff, ",", "", column++))) { int new_what; - int fracsec; buff = NULL; @@ -152,11 +151,10 @@ stmwpp_data_read(void) break; case 6: - sscanf(c, "%d:%d:%d.%d", &time.tm_hour, &time.tm_min, &time.tm_sec, &fracsec); - microseconds = MILLI_TO_MICRO(fracsec); + sscanf(c, "%d:%d:%d.%d", &time.tm_hour, &time.tm_min, &time.tm_sec, &milliseconds); /* makes sense only for recorded trackpoints */ if (what != STM_TRKPT) { - microseconds = 0; + milliseconds = 0; } break; @@ -167,7 +165,7 @@ stmwpp_data_read(void) if (wpt != NULL) { time.tm_year -= 1900; time.tm_mon--; - wpt->SetCreationTime(mkgmtime(&time), microseconds); + wpt->SetCreationTime(mkgmtime(&time), milliseconds); switch (what) { case STM_WAYPT: @@ -275,7 +273,7 @@ stmwpp_waypt_cb(const waypoint *wpt) gbfprintf(fout, ".%02d", 0); break; case STM_TRKPT: - gbfprintf(fout, ".%03d", MICRO_TO_MILLI(wpt->microseconds)); + gbfprintf(fout, ".%03d", wpt->GetCreationTime().msec()); break; } gbfprintf(fout, ",\r\n"); diff --git a/gpsbabel/unicsv.cc b/gpsbabel/unicsv.cc index 3086d1bd8..f1d8c0e14 100644 --- a/gpsbabel/unicsv.cc +++ b/gpsbabel/unicsv.cc @@ -429,7 +429,7 @@ unicsv_parse_time(const char *str, int *msec, time_t *date) ct = sscanf(str, "%d%1[.://]%d%1[.://]%d%lf", &hour, sep, &min, sep, &sec, &ms); is_fatal(ct < 5, MYNAME ": Could not parse time string (%s).\n", str); if (ct == 6) { - *msec = (ms * 1000000) + 0.5; + *msec = lround((ms * 1000000)); if (*msec > 999999) { *msec = 0; sec++; @@ -1192,7 +1192,7 @@ unicsv_parse_one_line(char *ibuf) } if (msec >= 0) { - wpt->microseconds = msec; + wpt->creation_time.addMSecs(msec); } if (opt_utc) { diff --git a/gpsbabel/vitosmt.cc b/gpsbabel/vitosmt.cc index bee5e1f39..a7d46dcdc 100644 --- a/gpsbabel/vitosmt.cc +++ b/gpsbabel/vitosmt.cc @@ -168,8 +168,8 @@ vitosmt_read(void) tmStruct.tm_sec =(int)floor(seconds); tmStruct.tm_isdst =-1; - wpt_tmp->SetCreationTime(mkgmtime(&tmStruct), - fmod(1000000*seconds+0.5,1000000)); + double usec = fmod(1000000*seconds+0.5,1000000); + wpt_tmp->SetCreationTime(mkgmtime(&tmStruct),lround(usec/1000.0)); wpt_tmp->shortname = (char*) xcalloc(16,1); snprintf(wpt_tmp->shortname, 15 , "WP%04d", ++serial); diff --git a/gpsbabel/vitovtt.cc b/gpsbabel/vitovtt.cc index f486a6488..a8eeae640 100644 --- a/gpsbabel/vitovtt.cc +++ b/gpsbabel/vitovtt.cc @@ -26,6 +26,7 @@ #include #include #include +#include #define MYNAME "vitovtt" #include "defs.h" @@ -68,7 +69,6 @@ vitovtt_read(void) double altitude = 0; struct tm tmStruct; int scaled_sec = 0; - int microseconds = 0; double speed = 0; int course = 0; int status = 0; @@ -110,9 +110,8 @@ vitovtt_read(void) wpt_tmp->altitude = altitude; tmStruct.tm_sec = scaled_sec / vitovtt_secondscale; - microseconds = (scaled_sec % vitovtt_secondscale) / vitovtt_microsecondscale; - wpt_tmp->SetCreationTime(mkgmtime(&tmStruct), microseconds); - + int microseconds = (scaled_sec % vitovtt_secondscale) / vitovtt_microsecondscale; + wpt_tmp->SetCreationTime(mkgmtime(&tmStruct), lround(microseconds/1000.0)); /* * TODO: interpret speed, course, status */ -- 2.30.2